<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateStaffOpenidAPIRequest;
use App\Http\Requests\API\UpdateStaffOpenidAPIRequest;
use App\Models\StaffOpenid;
use App\Models\StaffInfo;

use App\Repositories\StaffOpenidRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class StaffOpenidController
 * @package App\Http\Controllers\API
 */

class StaffOpenidAPIController extends AppBaseController
{
    /** @var  StaffOpenidRepository */
    private $staffOpenidRepository;

    public function __construct(StaffOpenidRepository $staffOpenidRepo)
    {
        $this->staffOpenidRepository = $staffOpenidRepo;
    }

    /**
     * Display a listing of the StaffOpenid.
     * GET|HEAD /staffOpenids
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->staffOpenidRepository->pushCriteria(new RequestCriteria($request));
        $this->staffOpenidRepository->pushCriteria(new LimitOffsetCriteria($request));
        $staffOpenids = $this->staffOpenidRepository->all();

        return $this->sendResponse($staffOpenids->toArray(), 'Staff Openids retrieved successfully');
    }

    /**
     * Store a newly created StaffOpenid in storage.
     * POST /staffOpenids
     *
     * @param CreateStaffOpenidAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateStaffOpenidAPIRequest $request)
    {
        $input = $request->all();

        // $uid = $request->input('uid');
        // if (!StaffInfo::where('id', $uid)->first()) {
        //     return $this->sendError('Staff not found');
        // }

        $staffOpenids = $this->staffOpenidRepository->create($input);

        return $this->sendResponse($staffOpenids->toArray(), 'Staff Openid saved successfully');
    }

    /**
     * Display the specified StaffOpenid.
     * GET|HEAD /staffOpenids/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var StaffOpenid $staffOpenid */
        $staffOpenid = $this->staffOpenidRepository->findWithoutFail($id);

        if (empty($staffOpenid)) {
            return $this->sendError('Staff Openid not found');
        }

        return $this->sendResponse($staffOpenid->toArray(), 'Staff Openid retrieved successfully');
    }

    /**
     * Update the specified StaffOpenid in storage.
     * PUT/PATCH /staffOpenids/{id}
     *
     * @param  int $id
     * @param UpdateStaffOpenidAPIRequest $request
     *
     * @return Response
     */
    public function update($openid, UpdateStaffOpenidAPIRequest $request)
    {
        $input = $request->all();

        /** @var StaffOpenid $staffOpenid */
        $staffOpenid = $this->staffOpenidRepository->findByField('openid', $openid)->first();

        if (empty($staffOpenid)) {
            return $this->sendError('Staff Openid not found');
        }

        $staffOpenid = $this->staffOpenidRepository->update($input, $staffOpenid->id);

        return $this->sendResponse($staffOpenid->toArray(), 'StaffOpenid updated successfully');
    }

    /**
     * Remove the specified StaffOpenid from storage.
     * DELETE /staffOpenids/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var StaffOpenid $staffOpenid */
        $staffOpenid = $this->staffOpenidRepository->findWithoutFail($id);

        if (empty($staffOpenid)) {
            return $this->sendError('Staff Openid not found');
        }

        $staffOpenid->delete();

        return $this->sendResponse($id, 'Staff Openid deleted successfully');
    }
}
